<?php


namespace app\admin\logic;

use app\admin\model\Dispatch as DispatchModel;

class Dispatch extends BaseLogic
{

    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new DispatchModel();

    }

    function updatePorject($year,$params,$value)
    {
        if($year == 2020)
            $projectmodel = new \app\admin\model\Project();
        if($year == 2021)
            $projectmodel = new \app\admin\model\Project_2021();

        $row = $projectmodel->where(['pno|name'=>$value])->find();
        if(!$row)
            return false;
        $status = 0;
        $updateData = [];
        foreach ($params as $key => $value) {
            if(!$value){

                if(isset($re_params[$key]) && $re_params[$key]==1){
                    $updateData['tmp_'.$key] = null;
                    $updateData['tmp_'.$key.'_status'] = 100;
                    $status = 1;
                }
                continue;
            }
            if(isset($row -> $key) &&  $params[$key] != $row[$key]){
                if(is_int($row -> $key) && (substr($key,-5) == '_time') && date("Y-m-d",$row -> $key) == $params[$key]) continue;
                $status = 1;
                $updateData['tmp_'.$key] = $params[$key];
                $updateData['tmp_'.$key.'_status'] = 100;

                $updateData['tmp_'.$key.'_submit_desc'] = '旬调度修改';


                if($key == 'framework_agreement_time' || $key == 'invesment_agreement_time')
                    $updateData['tmp_'.$key.'_desc'] = $params[$key];

                if($key=='project_source_ids')
                {
                    $activity = (new \app\admin\model\Activity)->where(['id'=>$value])->find();
                    $updateData['tmp_project_source_names']=$activity['name'];
                    $updateData['is_important'] = $activity['is_important'];
                }
            }
        }
        $updateData['status'] = $status;
        $row -> allowField(true) -> save($updateData);
    }


    public function addProject($value,\app\admin\model\dispatch $row) {

        //投资额大于1转签约库，签约日期
        if($row['investment_total']<1 && $row['is_important'] == 0)
            return false;
        $year = 0;
        $maxtime = max($row['invesment_agreement_time'],$row['framework_agreement_time']);
        if($maxtime>0)
            $year = date('Y',$maxtime);

        if($year ==0)
            return false;

        $projectmodel = null;
        if($year == 2020)
            $projectmodel = new \app\admin\model\Project();
        if($year == 2021)
            $projectmodel = new \app\admin\model\Project_2021();
        if(!$projectmodel)
            return false;

        $pro_row = $projectmodel->where(['pno|name'=>$value])->find();
        $ProjectData = [
            'name' => $row['name'],
            'investment_total'  => $row['investment_total'],
            'framework_agreement_time' => $row['framework_agreement_time'],
            'framework_agreement_time_desc' => is_numeric($row['framework_agreement_time'])?date('Y-m-d',$row['framework_agreement_time']):$row['framework_agreement_time'],
            'tmp_framework_agreement_time' =>null,
            'tmp_framework_agreement_time_status' =>null,
            'tmp_framework_agreement_time_submit_desc' =>null,
            'invesment_agreement_time' => $row['invesment_agreement_time'],
            'invesment_agreement_time_desc' => is_numeric($row['invesment_agreement_time'])?date('Y-m-d',$row['invesment_agreement_time']):$row['invesment_agreement_time'],
            'tmp_invesment_agreement_time' =>null,
            'tmp_invesment_agreement_time_status' =>null,
            'tmp_invesment_agreement_time_submit_desc' =>null,
            'project_source_ids'=>$row['project_source_ids'],
            'project_source_names'=>$row['project_source_names'],
            'tmp_project_source_ids'=>null,
            'tmp_project_source_names'=>null,
            'tmp_project_source_ids_status' =>null,
            'is_important' => $row['is_important']
        ];

        if($pro_row)
        {
            $ProjectData['convert_memo'] ='旬调度更新';
            $pro_row->allowField(true)->save($ProjectData);
        }
        else{
            $ProjectData['chargedep_ids'] = $row['settle_street_ids'];
            $ProjectData['chargedep_names'] = $row['settle_street_names'];
            $ProjectData['settle_street_ids'] = $row['settle_street_ids'];
            $ProjectData['settle_street_names'] = $row['settle_street_names'];
            $ProjectData['library_id']=SIGNED;
            $ProjectData['library_name'] ='签约库';
            $ProjectData['status' ] = -1;
            $ProjectData['is_visible'  ] = 0;
            $ProjectData['is_add'] =1;
            $ProjectData['is_convert'] =1;
            $ProjectData['convert_memo'] ='旬调度新增';
            $ProjectData['create_time' ] = time();
            $ProjectData['update_time'   ] = time();
            $ProjectData['year'] = $year;
            $ProjectData['pno'] = $row['project_pno'];
            $projectmodel->allowField(true)->save($ProjectData);
        }


    }





}
